package com.course.system.controller;

import com.course.server.dto.ResponseDto;
import com.course.server.exception.BusinessException;
import com.course.server.exception.ValidatorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ClassName: ControllerExceptionHandler
 * Description:
 * date: 2021/8/25 16:45
 *
 * @author 17878
 * @since JDK 1.8
 */
@Slf4j
@ControllerAdvice//进行异常处理的一个注解
public class ControllerExceptionHandler {
    @ExceptionHandler(value = BusinessException.class)//捕获异常明确
    @ResponseBody
    public ResponseDto businessExceptionHandler(BusinessException e) {
        ResponseDto responseDto = new ResponseDto();
        responseDto.setSuccess(false);
        log.error("业务异常：{}", e.getCode().getDesc());//异常码的中文描述
        responseDto.setMessage(e.getCode().getDesc());
        return responseDto;
    }
    @ExceptionHandler(value = ValidatorException.class)
    @ResponseBody
    public ResponseDto validatorExceptionHandler(ValidatorException e) {
        ResponseDto responseDto = new ResponseDto();
        responseDto.setSuccess(false);
        log.warn(e.getMessage());
        responseDto.setMessage("请求参数异常！");
        return responseDto;
    }
}
